######################################################################
# Time-stamp: <05/08/11 16:13:49 ostolop>
#
# Routines to validate User Management queries
######################################################################
package EP::EPC::Interface::Validator::User_Management;
use strict;

use EP::Common;
use EP::Common::Error qw ( :try );

use EP::Common::DBI;
use EP::Common::DBI::User;

my $COMPID = 0;

sub new {
  my $proto = shift;
  my $class = ref($proto) || $proto;
  my $self  = {};
  bless $self, $class;
  return $self;
}

sub validate {
  my $self = shift;
  my ($component, $source) = @_;

  my $err_code = "";

  try {
    if ( $component->findvalue('section[@id=0]/@active_subsection_id') == 2 ) {
      checkValidUsername  ( $component, $source );
      checkUsernameTaken  ( $component, $source );
      checkNewPassword_x2 ( $component, $source );
      checkProperPassword ( $component, $source );
    }
  } catch EP::Common::Error with {
    $err_code = shift;
  };

  return $err_code;
}

sub checkValidUsername {
  my ($component, $source) = @_;

  my $username = $component->findvalue ('section[@id=0]/subsection[@id=2]/submit[@name=\'username___c_0_s_0_su_2\']/value');
  throw EP::Common::Error ( -value => UM_INVALID_CHARS_IN_USERNAME ) if $username =~ /[[:^alnum:]]/;
}

sub checkUsernameTaken {
  my ($component, $source) = @_;

  my $username = $component->findvalue ('section[@id=0]/subsection[@id=2]/submit[@name=\'username___c_0_s_0_su_2\']/value');

  if ( EP::Common::DBI::User->search ( username => $username ) ) {
    throw EP::Common::Error ( -value => UM_USERNAME_ALREADY_TAKEN, -err_args => [$username] );
  }
}

sub checkNewPassword_x2 {
  my ($component, $source) = @_;

  # if we are creating a new user, test for typed in passwords to agree
  my $password1 = $component->find ('section[@id=0]/subsection[@id=2]/submit[@name=\'password___c_0_s_0_su_2\']/value')->pop();
  my $password2 = $component->find ('section[@id=0]/subsection[@id=2]/submit[@name=\'password_chk___c_0_s_0_su_2\']/value')->pop();

  if ( not $password1->textContent eq $password2->textContent ) {
    my $value = $source->createElement("value");
    my $value_text = $source->createTextNode ( "" );
    $value->appendChild ( $value_text );

    my $pn = $password1->parentNode;
    $pn->setAttribute ( "missing", 1 );
    $pn = $password2->parentNode;
    $pn->setAttribute ( "missing", 1 );

    $password1->replaceNode ( $value );
    $password2->replaceNode ( $value );

    throw EP::Common::Error ( -value => UM_PASSWORDS_DO_NOT_MATCH );
  }
}

sub checkProperPassword {
  my ($component, $source) = @_;

  # if we are creating a new user, test for typed in passwords to agree
  my $password1 = $component->find ('section[@id=0]/subsection[@id=2]/submit[@name=\'password___c_0_s_0_su_2\']/value')->pop();
  my $password2 = $component->find ('section[@id=0]/subsection[@id=2]/submit[@name=\'password_chk___c_0_s_0_su_2\']/value')->pop();

  my $min_length = $source->findvalue ('//component[@id='.$COMPID.']/section[@id=0]/subsection[@id=2]/input[@name=\'password\']/@min_length');

  if ( length ( $password1->textContent ) < $min_length ) {
    my $value = $source->createElement("value");
    my $value_text = $source->createTextNode ( "" );
    $value->appendChild ( $value_text );

    my $pn = $password1->parentNode;
    $pn->setAttribute ( "missing", 1 );
    $pn = $password2->parentNode;
    $pn->setAttribute ( "missing", 1 );

    $password1->replaceNode ( $value );
    $password2->replaceNode ( $value );

    throw EP::Common::Error ( -value => UM_PASSWORD_TOO_SHORT, -err_args => [$min_length] );
  }
}

1;

